/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-14
 * @Time  : 下午10:38
 * @brief :
 */

var createUi = require("common:widget/ui/base/base.js").createUi;

exports.Digital = createUi({
    _data:{
        container:'',
        number:0,
        len:8,
        type:'max'
    },

    _create:function(){
        var container = this.data('container');
        this.data('$container',$(container));
        this._initNumber();
        this._createTpl();
    },

    _init:function(){
        var $container = this.data('$container'),
            span = this.data('type')=="max"?75:50;

        $(window).on("scroll",function(){
            setTimeout(function(){
                var offset = $container.offset(),
                    oTop = offset.top,
                    sTop = $(window).scrollTop(),
                    wHeight = $(window).height();
                if(oTop>sTop && oTop<sTop+wHeight){

                    var $number = $container.find('.number-slider');
                    $.each($number,function(k,e){
                        $(e).css({
                            'top':-(span*$(e).attr('data-no'))
                        });
                    });
                }
            },500);
        }).trigger("scroll");
    },

    _initNumber:function(){
        var number = this.data('number'),
            len = this.data('len'),nlen;
        number = parseInt(number);
        if(!number || number<0){
            number = 0;
        }
        number = number.toString();
        nlen = number.length;
        if(len){
            for(i=0;i<len-nlen;i++){
                number='0'+number;
            }
        }
        this.data('number',number);
    },

    _createTpl:function(){
        var number = this.data('number'),
            $container = this.data('$container'),
            nlen = number.length,
            type = this.data('type'),
            html = '<div class="ui-digital '+(type=='min'?'ui-digital-min':'')+'"> ';

        for(var i=0;i<nlen;i++){
            var index = nlen- i,
                n = number.substring(i,i+1);
            if(index%3==0 && index!=0 && index<nlen){
                html+='<span class="daohao"></span>';
            }
            html+='' +
                '<div class="number-container">' +
                '<div class="number-slider" data-no='+n+'>' +
                '<span>'+this.createNumber(n)+'</span>' +
                '</div>' +
                '<i class="number-bg"></i>' +
                '</div>';

        }
        html+='</div>';
        $container.html(html);
    },

    createNumber:function(n){
        var tpl = '';
        for(var j=0;j<=n;j++){
            tpl+='<span>'+j+'</span>'
        }
        return tpl;
    }

});
